package com.nd.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.query.MPJLambdaQueryWrapper;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.nd.common.result.ResultUtils;
import com.nd.common.utils.EmptyUtils;
import com.nd.entity.OrderDTO;
import com.nd.entity.OrderDetail;
import com.nd.entity.Orders;
import com.nd.entity.Orders;
import com.nd.service.IOrdersService;
import com.nd.service.IOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liruirui
 * @since 2024-12-25
 */
@RestController
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    IOrdersService service;

    @PostMapping("/")
    public Object addOrder(@RequestBody Orders orders)
    {
        if(service.addOrder(orders))
        {
            return ResultUtils.returnSuccess();
        }
        else
        {
            return ResultUtils.returnFail("添加失败");
        }
    }

    @GetMapping("/front")
    public Object getOrderList(@RequestParam Integer deskId){
        MPJLambdaWrapper<Orders> wrapper = JoinWrappers.lambda(Orders.class)
                .selectAll(Orders.class)//查询订单表全部字段
                .selectCollection(OrderDetail.class,Orders::getOrderDetailList) //查询明细表中的数据
                .leftJoin(OrderDetail.class, OrderDetail::getOrderId, Orders::getId); //两表连查的条件
        //指定桌号
        wrapper.eq("desk_id",deskId);
        //按照订单时间倒序
        wrapper.orderByDesc("order_time");
        List<Orders> list = service.selectJoinList(Orders.class, wrapper);
        return ResultUtils.returnDataSuccess(list);
    }




    /**
     * 多条件分页查询
     * @param o 条件
     * @return
     */
    @GetMapping("/")
    public Object getList(OrderDTO o){
        //1.声明条件封装对象
        QueryWrapper qw = new QueryWrapper();
        //2.判断客户端传来的条件参数是否为空，若不为空则作为查询的条件
        if(EmptyUtils.isNotEmpty(o.getStates())){
            qw.eq("states",o.getStates());
        }
        if(EmptyUtils.isNotEmpty(o.getPriceMin())){
            qw.gt("total",o.getPriceMin());
        }
        if(EmptyUtils.isNotEmpty(o.getPriceMax())){
            qw.lt("total",o.getPriceMax());
        }
        if(EmptyUtils.isNotEmpty(o.getTimeMin())){
            qw.gt("order_time",o.getTimeMin());
        }
        if(EmptyUtils.isNotEmpty(o.getTimeMax())){
            qw.lt("order_time",o.getTimeMax());
        }
        //设置订单按照时间倒序排序
        qw.orderByDesc("order_time");
        //设置订单分页
        Page p = new Page(1,10);
        if(EmptyUtils.isNotEmpty(o.getPage()) && EmptyUtils.isNotEmpty(o.getLimit())){
            p = new Page(o.getPage(),o.getLimit());
        }


        //调用service层的方法实现：多条件分页查询
        Page page = service.page(p, qw);
        int total = (int)page.getTotal(); //获取总条数
        List records = page.getRecords(); //获取当前页的数据
        return ResultUtils.returnSuccessLayui(records,total);
    }



    /**
     * 修改
     * @param orders
     * @return
     */
    @PutMapping("/")
    public Object update(@RequestBody Orders orders){
        if(service.updateById(orders)){
            return ResultUtils.returnSuccess();
        }else{
            return ResultUtils.returnFail("修改失败");
        }
    }


}
